package com.lqq.chapter10.pool;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

/**
 * @Author lqq677
 * @Date 2021/8/12 16:03
 * @Description 演示线程池三种常见分类
 */
public class ThreadPoolDemo1 {

    //办理业务
    public static void execute(int n, ExecutorService threadPool) {

        try {
            for (int i = 1; i <= n; i++) {//模拟10个顾客请求
                //执行
                threadPool.execute(() -> {
                    System.out.println(Thread.currentThread().getName() + " 正在办理业务...");
                });
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            //关闭
            threadPool.shutdown();
        }

    }

    public static void main(String[] args) throws InterruptedException {

        //一池N线程
        ExecutorService threadPool1 = Executors.newFixedThreadPool(5);//一池5线程，比如说银行5个窗口
        execute(10, threadPool1);

        TimeUnit.SECONDS.sleep(1);
        System.out.println("------------分割线-------------");

        //一池一线程
        ExecutorService threadPool2 = Executors.newSingleThreadExecutor();
        execute(10, threadPool2);

        TimeUnit.SECONDS.sleep(1);
        System.out.println("------------分割线------------");

        //一池可扩容线程
        ExecutorService threadPool3 = Executors.newCachedThreadPool();
        execute(10, threadPool3);

        }
    }
